//
//  ActionBridge.m
//  jskick
//
//  Created by Andrew Barry on 18/02/09.
//  Copyright 2009 Barry Computer Services. All rights reserved.
//

#import "ActionBridge.h"

@implementation ActionBridge

-(id)initWithObject: (jsvalue) o withEvent: (NSString *) event withContext: (jscontext *) context withTarget:(jsvalue) t {
	cx = context;
	obj = o;
	target = t;
	eventName = event;
	[eventName retain];
	jsaddroot(cx, &obj);
	jsaddroot(cx, &target);
	return [super init];
}

-(void)dealloc {
	jsremoveroot(cx, &obj);
	jsremoveroot(cx, &target);

	[super dealloc];
}

-(void)doIt:(NSNotification *)notify {
	jsvalue fn = jsget(cx, obj, 0, [eventName UTF8String]);
	if (fn != 0 && JSVAL_IS_OBJECT(fn)) {
		jsinvoke(cx, fn, target, nil, 0);
	}

	fn = jsget(cx, obj, 0, "EnableControls");
	if (fn != 0 && JSVAL_IS_OBJECT(fn)) {
		jsinvoke(cx, fn, target, nil, 0);
	}
}
@end
